'use strict';

const Controller = require('egg').Controller;
const { getCookieValue } = require('../public/processCookies');
const { axios } = require('../public/httpHelper');
const qs = require('qs');
const ulid = require('../public/ulid');
const configDefault = require('../../config/config.default')();
/**
* @controller MandateController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class MandateController extends Controller {
  async send() {
    /**
        * @summary send
        * @description send
        * @router post /send
        * @request body send value 传入参数
        */
    const result = {};
    const params = this.ctx.request.body;
    params.random = ulid.ulid();
    const response = await this.ctx.service.mandate.send(params);
    if (response.status === 200) {
      const resId = response.msg;
      const { id: tenantid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
      const config = {
        method: 'post',
        url: `${configDefault.authorization.url}/updateTenant`,
        data: qs.stringify({
          authorizeRequestId: resId,
          email: params.email,
          telephone: params.telephone,
          address: params.address,
          deviceLimitNumbers: params.users,
          companyname: params.name,
          period: params.period,
          unit: params.unit,
          random: params.random,
          tenantid,
        }),
      };
      const r = await axios(config);
      console.log(r);
      if (r.status === 200) {
        result.msg = 'send successful';
        result.flag = true;
      } else {
        result.msg = 'send to auth server fail';
        result.flag = false;
      }
    } else {
      result.msg = response.msg;
      result.flag = false;
    }
    global.log.desc = 'send Authorize message';
    this.ctx.body = {
      msg: result.msg,
      flag: result.flag,
    };
  }
  async get() {
    /**
        * @summary get
        * @description get
        * @router get /get
        */
    const { id } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    const config = {
      url: `${configDefault.authorization.url}/getTenant?tenantid=${id}`,
      method: 'get',
    };
    const response = await axios(config);
    if (response && response.data) {

      const { authorizeRequestId } = response.data;
      const result = await this.ctx.service.mandate.get(authorizeRequestId);
      global.log.desc = 'get authorization code';
      this.ctx.body = {
        msg: result,
      };
    } else {
      this.ctx.body = {
        msg: null,
      };
    }
  }
  async check() {
    /**
        * @summary check
        * @description check
        * @router post /check
        * @request body check value 传入参数
        */
    const params = this.ctx.request.body;
    const { id } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tenantid = id;
    const config = {
      url: `${configDefault.authorization.url}/getTenant?tenantid=${id}`,
      method: 'get',
    };
    const response = await axios(config);
    if (response && response.data) {
      params.random = response.data.random;
      params.companyURL = '';// 前端页面去掉了companyURL，这里赋值空字符串
    } else {
      this.ctx.body = {
        msg: 'can not get tenantinfo',
        flag: false,
      };
      return;
    }
    const unUsed = await this.ctx.service.mandate.check(params);
    if (unUsed) {
      const flag = await this.ctx.service.mandate.checkCode(params);
      if (flag) {
        await this.ctx.service.mandate.updateTenant(params, response.data);
        this.ctx.body = {
          msg: 'success',
          flag: true,
        };
      } else {
        this.ctx.body = {
          msg: 'code is wrong',
          flag: false,
        };
      }
    } else {
      this.ctx.body = {
        msg: 'Authorize code is used',
        flag: false,
      };
    }
    global.log.desc = 'check authorization code';
  }
  async singleMandate() {
    /**
        * @summary singlemandate
        * @description singlemandate
        * @router get /singlemandate
        */
    const user = getCookieValue(this.ctx.cookies, 'token', 'user');
    // const result = await this.ctx.service.mandate.singleMandate(user);
    this.ctx.body = {
      data: user,
    };
  }
}

module.exports = MandateController;
